package cn.com.checne.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.checne.model.domain.CompanyInfo;
import cn.com.checne.model.domain.LanguageInfo;


@Repository
public class CompanyInfoDao {
	protected static final Logger log = Logger.getLogger(CompanyInfoDao.class);

	/**
	 * create
	 * @param company
	 * @return
	 */
	public boolean create(CompanyInfo company) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String createSql = "insert into tbl_company(lg_id_fk,company_cate_id,company_content) values(?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(createSql);
			int i = 0 ;
			pstmt.setInt(++i,company.getLanguageId());
			pstmt.setInt(++i,company.getCateId());
			pstmt.setString(++i,company.getContent());
			pstmt.execute();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param company
	 * @return
	 */
	public boolean update(CompanyInfo company) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_company set lg_id_fk = ?,company_cate_id = ? ,company_content = ? where company_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,company.getLanguageId());
			pstmt.setInt(++i,company.getCateId());
			pstmt.setString(++i,company.getContent());
			pstmt.setInt(++i,company.getCompanyId());
			pstmt.executeUpdate();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * 
	 * @param companyId
	 * @return
	 */
	public CompanyInfo getCompanyInfo(int cateId,int languageId) {
		CompanyInfo company = new CompanyInfo();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_company where company_cate_id = ? and lg_id_fk = ?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, cateId);
			pstmt.setInt(2, languageId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<CompanyInfo> list = getResult(rs);
			company = (list != null && list.size() > 0 ) ? list.get(0) : company ;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return company;
	}
	
	public CompanyInfo getCompanyInfo(int companyId) {
		CompanyInfo company = new CompanyInfo();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_company where company_id = ?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, companyId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<CompanyInfo> list = getResult(rs);
			company = (list != null && list.size() > 0 ) ? list.get(0) : company ;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return company;
	}
	/**
	 * get rseult from resultSet
	 * @param rs
	 * @return
	 */
	public List<CompanyInfo> getResult(ResultSet rs) {
		List<CompanyInfo> list = new ArrayList<CompanyInfo>();
		try {
			CompanyInfo company = null ;
			while(rs.next()) {
				company = new CompanyInfo();
				company.setCompanyId(rs.getInt("company_id"));
				company.setCateId(rs.getInt("company_cate_id"));
				company.setContent(rs.getString("company_content"));
				company.setLanguageId(rs.getInt("lg_id_fk"));
				LanguageInfo lg = new LanguageInfoDao().getLanguageInfoById(company.getLanguageId());
				company.setLanguageInfo(lg);
				list.add(company);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}